package com.hcms.admin.controller.sys;

import com.hcms.admin.service.ISysAdminLogService;
import com.hcms.common.api.CommonPage;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.SysAdminLog;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台用户登录日志表 前端控制器
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/sysAdminLog")
public class SysAdminLogController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysAdminLogService sysAdminLogService;

    @ApiOperation("获取日志列表")
    @GetMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<CommonPage<SysAdminLog>>> index(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String method,
            @RequestParam(required = false) String address,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        logger.info("Run index()");
        List<SysAdminLog> list = sysAdminLogService.getList(username, method, address, pageNum, pageSize);
        return CommonResponse.ok(CommonPage.restPage(list));
    }

    @ApiOperation("获取日志详情")
    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysAdminLog>> view(@PathVariable Long id) {
        logger.info("Run view()");
        SysAdminLog entity = sysAdminLogService.getView(id);
        if (entity == null) {
            return CommonResponse.notFound("记录不存在");
        }
        return CommonResponse.ok(entity);
    }

}
